/**
	@author yangcaho message         
**/
YOUXIXIU = window.YOUXIXIU || {};
YOUXIXIU.MAIN= YOUXIXIU.MAIN || {};
YOUXIXIU.MAIN.BindAddAtten = (function($) {
	var html='';
	var status={notice:false,atme:false,unread:false}
	var notice_num,atme_num,unread_num;
	var opt={};
	var defaults={
		bind_class:".add_atten",
		tml_add_div:'<div  class="followStatusPlace add_div"><a class="add_atten follow_inviteBtn" href="#" nickname="">加关注</a></div>',
		tml_huxiang_div:'<div style="" class="W_addbtn_even huxiang_div"><span class="icon_huxiang "></span><i>|</i><a href="javascript:;" class="del_atten W_linkb unfollowBtn"><em>取消</em></a></div>',
		tml_yijing_div:'<div style="" class="W_addbtn_even yijing_div"><span class="icon_yijing ">已关注</span><i>|</i><a href="javascript:;" class="del_atten W_linkb unfollowBtn"><em>取消</em></a></div>'
	
	};
	//-----global functions ------// 
	function chkDefault(options)
	{
		for(key in defaults)
		{
			if(typeof(options[key])=='undefined')
			{
				options[key]=defaults[key];
			}
		}
		return options;
	}

	

	

	 function bind_add_atten()
	{
			var bind_class = opt.bind_class;
			$(bind_class).die("click");
			$(bind_class).live("click",function(e){
				
				uid = $(e.target).parents(".user_relation").attr("uid");
				var self = $(e.target);
				$.getJSON("/interface/user/json_add_atten.php?t=jsonp&type=add&uid="+uid+"&callback=?", function(json){
					if(typeof(json)!='undefined')
					{
						if(json.error == 0)
						{
							YOUXIXIU.MAIN.Tips.Show(json.errmsg);
							
							if(json.res == 0)
							{
								self.parents(".user_relation").html(opt.tml_add_div);	
							}
							else if(json.res == 1)
							{
								self.parents(".user_relation").html(opt.tml_yijing_div);	
								//self.parents(".user_relation").find(".yijing_div").show();	
							}
							else
							{
								self.parents(".user_relation").html(opt.tml_huxiang_div);	
								//self.parents(".user_relation").find(".huxiang_div").show();	
							}
						}
						else
						{
							
							YOUXIXIU.MAIN.Tips.Show(json.errmsg);
						}
						
					}
					
				});


					
			});

		
		}

	 function bind_del_atten()
	{
			$(".del_atten").die("click");
			$(".del_atten").live("click",function(e){
				
				uid = $(e.target).parents(".user_relation").attr("uid");
				
				var self = $(e.target);
				$.getJSON("/interface/user/json_add_atten.php?t=jsonp&type=del&uid="+uid+"&callback=?", function(json){
					if(typeof(json)!='undefined')
					{
						if(json.error == 0)
						{
							YOUXIXIU.MAIN.Tips.Show(json.errmsg);
							if(json.res == 0)
							{
								self.parents(".user_relation").html(opt.tml_add_div);	
							}
							else if(json.res == 1)
							{
								self.parents(".user_relation").html(opt.tml_yijing_div);	
								//self.parents(".user_relation").find(".yijing_div").show();	
							}
							else
							{
								self.parents(".user_relation").html(opt.tml_huxiang_div);	
								//self.parents(".user_relation").find(".huxiang_div").show();	
							}
							
						}
						else
						{
							
							YOUXIXIU.MAIN.Tips.Show(json.errmsg);
						}
						
					}
					
				});


					
			});

		
		}
	
	function bind_user_relation()
	{
			$(".user_relation").each(function(){
			
				uid = $(this).attr("uid");
				var self = $(this);
				$.getJSON("/interface/user/json_get_relation.php?t=jsonp&uid="+uid+"&callback=?", function(json){
					if(typeof(json)!='undefined')
					{
						if(json.error == 0)
						{
							if(json.res == 0)
							{
								self.html(opt.tml_add_div)	
							}
							else if(json.res == 1)
							{
								self.html(opt.tml_yijing_div)
							}
							else
							{
								
								self.html(opt.tml_huxiang_div)	
							}


						}
						else
						{
							
							//YOUXIXIU.MAIN.Tips.Show(json.errmsg);
						}
						
					}
					
				});
					
			});

		
		}



	
	return {
			init: function(options)
			{
				opt=chkDefault(options);
				bind_add_atten();
				bind_del_atten();
				bind_user_relation();
			},
			bind_add_atten:function(options){
				opt=chkDefault(options);
				return bind_add_atten();
			}
		};

})(jQuery);